<?php

namespace app\common\model\hfztcy;

use app\common\model\BaseModel;
use app\common\model\service\LibUploadCompressImage;
use common\Common;
use think\Exception;

//公共函数

/**
 * 配置表
 * @name LibConfig.php
 * @package lbh
 * @category model
 * @link http://www.chanekeji.com
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2022-06-17
 */
class LibConfig extends BaseModel
{
    protected $m_config;
    protected $table = 'hfztcy_config';

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->m_config = db($this->table);
        parent::__construct();
    }

    /**
     * 添加表单信息
     */
    public function addConfig($condition)
    {
        try {
            $reslut = $this->m_config->insertGetId($condition);
            if (true == $reslut) {
                return Common::errorArray(0, "添加成功", $reslut);
            } else {
                return Common::errorArray(1, "添加失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }


    /**
     * 查询单个表单信息
     * @param $condition
     * @param array $result
     */
    public function findConfig($condition = '', $field = '')
    {
        try {
            $reslut = $this->m_config->where($condition)->field($field)->find();

            if (true == $reslut) {
                return Common::errorArray(0, "查询成功", $reslut);
            } else {
                return Common::errorArray(1, "查询为空", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 查询多个表单信息
     * @param $condition
     * @param $limit
     * @param $field
     * @param $sort
     * @param array $result
     */
    public function findAllConfig($condition = '', $limit = '', $field = '', $sort = 'id desc')
    {
        try {
            $reslut = $this->m_config->where($condition)->order($sort)->limit($limit)->field($field)->select();
            if (true == $reslut) {
                return Common::errorArray(0, "查询成功", $reslut);
            } else {
                return Common::errorArray(1, "查询为空", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 更新表单信息
     * @param $condition
     * @param $userInfo
     * @param array $result
     */
    public function updateConfig($condition, $userInfo)
    {
        try {
            $reslut = $this->m_config->where($condition)->update($userInfo);
            if ($reslut >= 0) {
                return Common::errorArray(0, "更新成功", $reslut);
            } else {
                return Common::errorArray(1, "更新失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage() . $ex->getMessage(), $ex);
        }
    }

    /**
     * 删除表单信息
     * @param $condition
     * @param array $result
     */
    public function deleteConfig($ids)
    {
        $condition = array('id' => array('in', $ids));
        try {
            $reslut = $this->m_config->where($condition)->delete();;
            if (true == $reslut) {
                return Common::errorArray(0, "删除成功", $reslut);
            } else {
                return Common::errorArray(1, "删除失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 字段值加1
     * @param $condition
     * @param string $field
     * @param int $optval
     * @param array $reslut
     */
    public function increaseField($condition, $field, $optval = 1)
    {
        try {
            $reslut = $this->m_config->where($condition)->setInc($field, $optval);;
            if (true == $reslut) {
                return Common::errorArray(0, "修改成功", $reslut);
            } else {
                return Common::errorArray(1, "修改失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 字段值减1
     * @param $condition
     * @param string $field
     * @param int $optval
     * @param array $reslut
     */
    public function decreaseField($condition, $field, $optval = 1)
    {
        try {
            $reslut = $this->m_config->where($condition)->setDec($field, $optval);;
            if (true == $reslut) {
                return Common::errorArray(0, "修改成功", $reslut);
            } else {
                return Common::errorArray(1, "修改失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 分页获取数据表单信息
     */
    public function pagingConfig($page, $conditionList, $sort = '', $orList = null)
    {
        $reslut = Common::paging($this->table, $page, $conditionList, $sort, $orList);
        return $reslut;
    }

    /**
     * Info: 上传视频
     * Date: 2022/6/17 9:45
     */
    public function uploadVideo()
    {
        $file = request()->file('short_video');
        if ($file) {
            $dir = "upload/config/video/";
            $moveImage = $file->move(ROOT_PATH . 'public' . DS . $dir);
            if ($moveImage) {
                $name = $moveImage->getSaveName();
                $name = str_replace("\\", "/", $name);
                $new_path = $dir . $name;
                return Common::errorArray(0, "上传成功", ['path' => $new_path]);
            } else {
                // 上传失败获取错误信息
                return Common::errorArray(1, "上传失败", $file->getError());
            }
        } else {
            return Common::errorArray(2, "没有上传", false);
        }
    }

    /**
     * Info: 上传图片
     * Date: 2022/6/17 9:45
     */
    public function uploadPosterImg()
    {
        $res = (new LibUploadCompressImage('config/posters','posters_img'))->uploadImage();
        if ($res['errorCode'] == 0) {
            return Common::errorArray(0, "上传成功", ['path' => $res['data']]);
        }
        return $res;
    }

    /**
     * Info: 上传图片
     * Date: 2022/6/17 9:45
     */
    public function uploadRedMotherPosterImg()
    {
        $res = (new LibUploadCompressImage('config/posters','red_mother_posters_img'))->uploadImage();
        if ($res['errorCode'] == 0) {
            return Common::errorArray(0, "上传成功", ['path' => $res['data']]);
        }
        return $res;
    }

    /**
     * Info: 上传图片
     * Date: 2022/6/17 9:45
     */
    public function uploadCategoryPosterImg()
    {
        $res = (new LibUploadCompressImage('config/posters','category_posters_img'))->uploadImage();
        if ($res['errorCode'] == 0) {
            return Common::errorArray(0, "上传成功", ['path' => $res['data']]);
        }
        return $res;
    }

    /**
     * Info: 上传图片
     * Date: 2022/6/17 9:45
     */
    public function uploadMatchmakerImg()
    {
        $res = (new LibUploadCompressImage('config/matchmaker_pic','matchmaker_pic'))->uploadImage();
        if ($res['errorCode'] == 0) {
            return Common::errorArray(0, "上传成功", ['path' => $res['data']]);
        }
        return $res;
    }

    /**
     * Info: 上传图片
     * Date: 2022/6/17 9:45
     */
    public function uploadStoreImg()
    {
        $res = (new LibUploadCompressImage('config/stores_pic','stores_pic'))->uploadImage();
        if ($res['errorCode'] == 0) {
            return Common::errorArray(0, "上传成功", ['path' => $res['data']]);
        }
        return $res;
    }
}

?>